package hwOne3;

import edu.princeton.cs.algs4.Date;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdOut;

import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

/**
 * 作业 1.3.17
 */
public class Transaction {

    private String name;
    private Date when;
    private double amount;
    Transaction(String transaction) {
        String[] infoString = transaction.split(" ");
        StdOut.println("info" + Arrays.toString(infoString));
        name = infoString[0];

//        String[] dateStr = infoString[1].split("/");
        when = new Date(infoString[1]);
        amount = Integer.parseInt(infoString[2]);
    }

    public String who() {
        return name;
    }
    public String when() {
        return when.toString();
    }
    public double amount() {
        return amount;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    public static Transaction[] readDates() {
        Scanner scanner = new Scanner(System.in);
        edu.princeton.cs.algs4.Queue<String> q = new Queue<>();

        while (scanner.hasNext()) {
            String line = scanner.nextLine();
            if (Objects.equals(line, "end")) {
                scanner.close();
                break;
            }
            q.enqueue(line);
        }
        System.out.println("输入结束！");


        int N = q.size();
        Transaction[] container = new Transaction[N];
        for (int i = 0; i < N; i++) {
            String tran = String.valueOf(q.dequeue());
            container[i] = new Transaction(tran);
        }

        return container;
    }
    public String toString() {
        return name + ":" + when + "," + amount;
    }
    public static void main(String[] args) {
        Transaction[] trans = readDates();
        System.out.println("读取到的日期如下：");
        for (Transaction tran : trans) {
            System.out.println(tran);
        }
    }

}
